﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using YABE.Models;

namespace YABE
{
	public class GlobalApplication : System.Web.HttpApplication
	{
		public static void RegisterRoutes(RouteCollection routes)
		{
			// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
			//       automatic support on IIS6 and IIS7 classic mode
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.Add(new Route("Json/Posts", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Json", action = "Posts" }),
			});

			routes.Add(new Route("Json/Posts/{year}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Json", action = "PostsYear"
					, year = "" }),
			});

			routes.Add(new Route("Json/Posts/{year}/{month}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new
				{
					controller = "Json",
					action = "PostsMonth",
					year = "",
					month = ""
				}),
			});

			routes.Add(new Route("Json/{action}/{id}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Json", action = "", id = "" }),
			});

			routes.Add(new Route("Login", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Admin", action = "Login" }),
			});

			routes.Add(new Route("tag/{tag}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Posts", action = "ListByTag", tag = "uncategorized" }),
			});

			routes.Add(new Route("Posts/{action}/{id}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Posts", action = "List", id = "" }),
			});

			routes.Add(new Route("Admin/{action}/{id}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Admin", action = "List", id = "" }),
			});

			routes.Add(new Route("Feeds/{action}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Feeds", action = "rss20" }),
			});

			routes.Add(new Route("Feed/{action}", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Feeds", action = "rss20" }),
			});

			routes.Add(new Route("{year}", new MvcRouteHandler())
			{
                 Constraints = new RouteValueDictionary(new { year = @"\d{4}"}),
				Defaults = new RouteValueDictionary(new
				{
					controller = "Posts",
					action = "ListYear",
					year = ""
				}),
			});

			routes.Add(new Route("{year}/{month}", new MvcRouteHandler())
			{
                Constraints = new RouteValueDictionary(new { year = @"\d{4}", month = @"[1-9]|0[1-9]|1[012]" }),
				Defaults = new RouteValueDictionary(new
				{
					controller = "Posts",
					action = "ListMonth",
					year = "",
					month = ""
				}),
			});

			routes.Add(new Route("{year}/{month}/{day}", new MvcRouteHandler())
			{
                Constraints = new RouteValueDictionary(new { year = @"\d{4}", month = @"[1-9]|0[1-9]|1[012]", day = @"[1-9]|0[1-9]|[12][0-9]|3[01]" }),
				
				Defaults = new RouteValueDictionary(new
				{
					controller = "Posts",
					action = "ListDay",
					year = "",
					month = "",
					day = ""
				}),
			});

			routes.Add(new Route("{year}/{month}/{day}/{title}", new MvcRouteHandler())
			{
                Constraints = new RouteValueDictionary(new { year = @"\d{4}", month = @"[1-9]|0[1-9]|1[012]", day = @"[1-9]|0[1-9]|[12][0-9]|3[01]" }),
				Defaults = new RouteValueDictionary(new
				{
					controller = "Posts",
					action = "Single",
					year = "",
					month = "",
					day = "",
					title = ""
				}),
			});

			routes.MapRoute("ContentShow",
				"Content/{contentType}",
				new { controller = "Content", action = "Show", contentType = "Index" }
				);

			routes.MapRoute("Content",
				"Content/{action}/{id}",
				new { controller = "Content", action = "Index", id = "" }
				);

		    //routes.MapRoute("UnknownController", "{*values}",
		      //              new {controller = "Posts", action = "Index"});

            //routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())
            //{
                
            //    Defaults = new RouteValueDictionary(new {controller="Posts",action = "Index", id = "" }),
            //});

			routes.Add(new Route("Default.aspx", new MvcRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Posts", action = "List" }),
			});
		}

		protected void Application_Start(object sender, EventArgs e)
		{
			RegisterRoutes(RouteTable.Routes);
			//Routing.SetAppRoutes();
		}
	}
}